#include "aky_com_include.h"

#define PUMP_MAX_DUTY (2000ul)
#define VALVE_MAX_DUTY (2000ul)
uint8 BB;

void set_pump_duty(uint16_t duty)
{
	TC1D = 0XFFFF - (PUMP_MAX_DUTY - duty);				//Init load data 
}
void set_valve_duty(uint16_t duty)
{
	TC0D = 0XFFFF - (VALVE_MAX_DUTY - duty);				//Init load data 
}

void aky_pwm_init(void)
{
	P0M  |=	(0x01 << 7); //pump 引脚输出
	P0UR  &= ~(0x01 << 7);

	TC1C = 65536 - PUMP_MAX_DUTY;				//Init load data 
	TC1R = 65536 - PUMP_MAX_DUTY;				//Init re-load data 
	TC1D = 65536 - PUMP_MAX_DUTY;				//Init duty cycle data
	TC1M = 0x76;	//01010110	//	16khz

    VALVE_PROT_REG |= VALVE_PIN;// IO端口方向
    VALVE_UP_REG &= ~VALVE_PIN;// 上拉配置

    // if (g_s_func.e_valve_type == VALVE_LINEAR)
    // {
    // 	TC0C = 65536 - VALVE_MAX_DUTY;				//Init load data 
    // 	TC0R = 65536 - VALVE_MAX_DUTY;				//Init re-load data 
    // 	TC0D = 65536 - VALVE_MAX_DUTY;				//Init duty cycle data
	// 	TC0M = 0x76;	//84				
    // }
    // else //快速阀
    {
        VALVE_OFF;
    }

#if (AKY_DEBUG_UART_PRINTF == 1)
    aky_uart_send_string("aky_pwm_init\n");
#endif

}



//根据最大值(ful_range)和比例(per)数值关系输出pwm到通道
//(e_pwm = pump, valve)
//ful_range 最大值
//per 比例
void pwm_out_put(uint16_t per, uint16_t ful_range,e_pwm_t e_pwm)
{
    volatile uint32_t pwm_val = 0;

    if (e_pwm == VALVE_DUTY)
    {
        // if (g_s_func.e_valve_type == VALVE_FAST)//快速阀不输出pwm
        {
             
            if (per > 0)//100%
            {
            	VALVE_H;    // 
            }
            else if (per == 0)//0%
            {
                VALVE_L;            //
            }

            return;
        }

        // if (per == 0)//0%
        // {
        //     set_valve_duty(0);
        // 	VALVEPWM_DIS;                      // PWM使能
        // }        
        // else if (ful_range == per)//100%
        // {
        //     set_valve_duty(VALVE_MAX_DUTY);
        // 	VALVEPWM_EN;                      // PWM使能
        // }
        // else
        // {
        //     pwm_val = (VALVE_MAX_DUTY * per ) / ful_range;
        //     set_valve_duty(pwm_val);
        // 	VALVEPWM_EN;                      // PWM使能
        // }
    }
    else if (e_pwm == PUMP_DUTY)
    {
        if (per == 0)//0%
        {
            set_pump_duty(0);
        	PUMPPWM_DIS;                      // PWM使能
        }
        else if (ful_range == per)//100%
        {
            set_pump_duty(PUMP_MAX_DUTY);
        	PUMPPWM_EN;                      // PWM使能
        }
        else
        {
            pwm_val = (PUMP_MAX_DUTY * per ) / ful_range;
            set_pump_duty(pwm_val);
        	PUMPPWM_EN;                      // PWM使能
        }
    }
}


void iocontrolon(void)
{
    if (BB >= 0x80)
    {
        BB -= 0x80;
    }
    if (BB >= 0x40)
    {
        BB -= 0x40;
        // OUT_POWER=0;
    }
    if (BB >= 0x20)
    {
        BB -= 0x20;
        PUMPPWM_EN;
    }
    if (BB >= 0x10)
    {
        // if (g_s_func.e_valve_type == VALVE_LINEAR)
        // {
        //     pwm_out_put(100,100,VALVE_DUTY);
        //     VALVEPWM_EN;
        // }
        // else
        {
            // OUT_VALVE=1;
            VALVE_ON;
        }
    }
}
// /************************************************/
void iocontroloff(void)
{
    if (BB >= 0x80)
    {
        BB -= 0x80;
    }
    if (BB >= 0x40)
    {
        // OUT_POWER=1;
        BB -= 0x40;
    }
    if (BB >= 0x20)
    {
        BB -= 0x20;
        PUMPPWM_DIS;
    }
    if (BB >= 0x10)
    {
        // if (g_s_func.e_valve_type == VALVE_LINEAR)
        // {
        //     pwm_out_put(0,100,VALVE_DUTY);
        //     VALVEPWM_DIS;
        // }
        // else
        {
            // OUT_VALVE=1;
            VALVE_OFF;
        }
    }
}
